﻿/// <reference path="jquery-1.6.2.js" />

function doSave() {
    g_ajaxSetup();

    var model = {
        Name: $('#txtName').val(),
        Description: $('#txtDescription').val(),
        CategoryId: $('#ddlCategory').val(),
        Price: $('#txtPrice').val()
    };

    $.ajax({
        url: '/Home/Save',
        type: "Post",
        traditional: true,
        async: false,
        data: JSON.stringify(model),
        contentType: 'application/json; charset=utf-8',
        dataType: "json",
        success: function (result) {
            window.location = "/Home/Products";
        },
        error: function (result) {
        }
    });
}

// function: ajax setup (bug fix in jquery ajax post)
function g_ajaxSetup() {
    $.ajaxSetup({
        jsonp: null,
        jsonpCallback: null
    });
}

function onEdit(id) {
    $.getJSON("/api/Product/" + id.toString(), function (result) {
    });
}
function onDelete(id) {
    $.ajax({
        url: '/api/Product/' + id.toString(),
        type: "DELETE",
        success: function (result) {
            window.location = "/home/products";
        },
        error: function (A, b, c) {
            alert(c);
        }
    });
}


function doCategorySave() {
    g_ajaxSetup();

    var model = {
        Name: $('#txtName').val(),
        Description: $('#txtDescription').val()
        
    };

    $.ajax({
        url: '/api/Category',
        type: "Post",
        traditional: true,
        async: false,
        data: JSON.stringify(model),
        contentType: 'application/json; charset=utf-8',
        dataType: "json",
        success: function (result) {
            window.location = "/Home/Category";
        },
        error: function (result) {
        }
    });
}
